package com.cipta.lomob;

import java.util.Arrays;

import org.json.JSONObject;

import tv.lomob.api.Lomob;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.cipta.lomob.base.BaseActivity;
import com.cipta.lomob.components.LomobFBLoginButton;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import tv.lomob.api.LomobResponseHandler;

public class LoginActivity extends BaseActivity {
	private Button btnTwitter, btnLomob;
	private LomobFBLoginButton btnFacebook;
	private TextView txtRegister;
	private UiLifecycleHelper uiHelper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		// prepare facebook routine
		uiHelper = new UiLifecycleHelper(this, new Session.StatusCallback() {

			@Override
			public void call(Session session, SessionState state,
					Exception exception) {
				if (session.isOpened() && state == SessionState.OPENED) {
					//show loading here
					Log.i("LOMOB", "get token from facebook");
				}
			}
		});
		uiHelper.onCreate(savedInstanceState);
		initializeViews();
		initializeProcess();
		initializeActions();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		uiHelper.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onPause() {
		uiHelper.onPause();
		super.onPause();
	}

	@Override
	protected void onResume() {
		uiHelper.onResume();
		super.onResume();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onStop() {
		uiHelper.onStop();
		super.onStop();
	}

	@Override
	public void initializeViews() {
		// TODO Auto-generated method stub
		super.initializeViews();

		txtRegister = (TextView) findViewById(R.id.txt_login);
		btnFacebook = (LomobFBLoginButton) findViewById(R.id.btn_login_fb);
		btnTwitter = (Button) findViewById(R.id.btn_login_twitter);
		btnLomob = (Button) findViewById(R.id.btn_login_lomob);
	}

	@Override
	public void initializeProcess() {
		// TODO Auto-generated method stub
		super.initializeProcess();

		String register = "Register here";
		txtRegister.setText(register);
		txtRegister.setTextColor(getResources().getColor(R.color.comand_text_color));
	}

	@Override
	public void initializeActions() {
		// TODO Auto-generated method stub
		super.initializeActions();

		// btnFacebook.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// // TODO Auto-generated method stub
		// ConnectFriendsActivity
		// .toConnectFriendsActivity(LoginActivity.this);
		// }
		// });
//		};
		
		btnFacebook.setUserInfoChangedCallback(new LomobFBLoginButton.UserInfoChangedCallback() {	
			@Override
			public void onUserInfoFetched(GraphUser user) {
				if(user !=null){
					//now check if user has registerd on server
					Lomob.authentificationFacebook(user.getId(), Session.getActiveSession().getAccessToken(), new LomobResponseHandler() {
						@Override
						public void onSuccess(final JSONObject result) {
							try{
								final String token = result.optString("token");
								//now load user profile from lomob server
								Lomob.getProfile(token, new LomobResponseHandler() {
									
									@Override
									public void onSuccess(JSONObject user) {
										try{
											user.put("token", token);
										}catch(Exception ex){
											ex.printStackTrace();
										}
										//set user profile
										LomobManager.getInstance().setUser(LoginActivity.this, user);
										MainActivity.toMainActivity(LoginActivity.this);
										finish();
									}
									
									@Override
									public void onError(String message) {
										//fail get user profile
										Log.e("LOMOB", message);
									}
								});
							}catch(Exception ex){
								Log.e("LOMOB", ex.getMessage());
								ex.printStackTrace();
							}
						}
						
						@Override
						public void onError(String message) {
							Log.e("LOMOB_AUTH", message);
						}
					});
				}
			}
		});
		
		btnTwitter.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ConnectFriendsActivity
						.toConnectFriendsActivity(LoginActivity.this);
			}
		});

		btnLomob.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				LomobLoginActivity.toLomobLoginActivity(LoginActivity.this);
				finish();
			}
		});

		txtRegister.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				RegisterActivity.toRegisterActivity(LoginActivity.this);
			}
		});
	}

	public static void toLoginActivity(Activity activity) {
		activity.startActivity(new Intent(activity, LoginActivity.class));
	}
	
}
